package com.Luoyi.springframework.core.io;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

//通过 HTTP 的方式读取云服务的文件
public class UrlResource implements Resource{
    private final URL url;

    public UrlResource(URL url) {
        this.url = url;
    }

    @Override
    public InputStream getInputStream() throws IOException {
        URLConnection con = this.url.openConnection();

        try{
            return con.getInputStream();
        }catch (IOException ex){
            if (con instanceof HttpURLConnection){
                //释放链接
                ((HttpURLConnection) con).disconnect();
            }throw ex;

        }

    }
}
